<?php
/**
 * Manange Category
 *
 * @author tuancasi
 */
class Category {
    const DEAL = 0;
    const FLIGHT = 1;   
    /**     
     * @param int $userId
     * @param string $name is category name
     * @param int $type is flight/deal
     */
    public static function add($userId, $name, $type){
	global $wpdb;
	$sql = 'INSERT INTO `travel_category`(`user_id`, `name`, `type`, `create_time`) 
	    VALUES('.$userId.', "'.$name.'", '.$type.', '.time().')';
	$wpdb->query($sql);
    }
    /**    
     * @param int $id
     */
    public static function getInfo($id){
	global $wpdb;
	$sql = 'SELECT * FROM `travel_category` WHERE `category_id` = '.$id;
	$data = $wpdb->get_results($sql);
	return $data;
    }
    /**
     *      
     * @param int $id
     * @param string $name
     */
    public static function update($id, $name){
	global $wpdb;
	$sql = 'UPDATE `travel_category` SET `name` = "'.$name.'" WHERE `category_id` = '.$id;	
	$wpdb->query($sql);
    }
    /**    
     * @param int $id
     */
    public static function delete($id){
	global $wpdb;
	$sql = 'DELETE FROM `travel_category` WHERE `category_id` = '.$id;
	$wpdb->query($sql);
    }   
    /**
     *      
     * @param int $type
     * @return array
     */
    public static function countRecords($type){
	global $wpdb;
	$sql = 'SELECT COUNT(`category_id`) AS total FROM `travel_category` WHERE `type` = '.$type;
	$data = $wpdb->get_results($sql);
	return $data;
    }
    /**
     * 
     * @param int $type
     * @param int $start
     * @param int $limit
     * @return array
     */
    public static function getAll($type, $start, $limit) {
        global $wpdb;	
        $aDesign = $wpdb->get_results('SELECT * FROM `travel_category` WHERE
	    type = ' . $type . ' ORDER BY `category_id` DESC limit ' . $start . ', ' . $limit);
        return $aDesign;
    }
    /*
     * Get All Category by Type
     * 
     * @author:Tham
     * @param int type
     * return listCategory
     */
    public static function  getAllCategoryByType($type)
    {
           global $wpdb;	
           $listCategory = $wpdb->get_results('SELECT * FROM `travel_category` WHERE  type = ' . $type );
           return $listCategory;
    }
}
